package com.example.myapplication.data.repository

import com.example.myapplication.data.database.StorageSpaceDao
import com.example.myapplication.data.database.toEntity
import com.example.myapplication.data.database.toModel
import com.example.myapplication.data.model.StorageSpace
import com.example.myapplication.data.model.StorageSpaceWithCount
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton

/**
 * 储物空间数据仓库接口
 */
interface StorageSpaceRepository {
    
    /**
     * 获取所有顶级储物空间
     */
    fun getAllTopLevelSpaces(): Flow<List<StorageSpace>>
    
    /**
     * 获取所有储物空间
     */
    fun getAllSpaces(): Flow<List<StorageSpace>> // New function
    
    /**
     * 根据父ID获取子储物空间
     */
    fun getChildSpaces(parentId: String): Flow<List<StorageSpace>>
    
    /**
     * 根据ID获取储物空间
     */
    suspend fun getSpaceById(id: String): StorageSpace?
    
    /**
     * 根据ID获取储物空间（Flow版本）
     */
    fun getSpaceByIdFlow(id: String): Flow<StorageSpace?>
    
    /**
     * 获取储物空间的完整路径
     */
    suspend fun getSpacePath(spaceId: String): List<StorageSpace>
    
    /**
     * 搜索储物空间
     */
    fun searchSpaces(query: String): Flow<List<StorageSpace>>
    
    /**
     * 插入或更新储物空间
     */
    suspend fun insertOrUpdateSpace(space: StorageSpace): Long
    
    /**
     * 删除储物空间
     */
    suspend fun deleteSpace(space: StorageSpace)
    
    /**
     * 根据ID删除储物空间
     */
    suspend fun deleteSpaceById(id: String)
    
    /**
     * 检查储物空间是否有子空间
     */
    suspend fun hasChildSpaces(spaceId: String): Boolean
    
    /**
     * 获取储物空间的物品数量
     */
    suspend fun getItemCount(spaceId: String): Int
    
    /**
     * 获取顶级储物空间及其物品数量
     */
    fun getTopLevelSpacesWithCounts(): Flow<List<StorageSpaceWithCount>>
}

/**
 * 储物空间数据仓库实现
 */
@Singleton
class StorageSpaceRepositoryImpl @Inject constructor(
    private val storageSpaceDao: StorageSpaceDao
) : StorageSpaceRepository {
    
    override fun getAllTopLevelSpaces(): Flow<List<StorageSpace>> {
        return storageSpaceDao.getAllTopLevelSpaces().map { entities ->
            entities.map { it.toModel() }
        }
    }
    
    override fun getTopLevelSpacesWithCounts(): Flow<List<StorageSpaceWithCount>> {
        return storageSpaceDao.getTopLevelSpacesWithItemCount()
    }
    
    override fun getAllSpaces(): Flow<List<StorageSpace>> { // New function
        return storageSpaceDao.getAllSpaces().map { entities ->
            entities.map { it.toModel() }
        }
    }
    
    override fun getChildSpaces(parentId: String): Flow<List<StorageSpace>> {
        return storageSpaceDao.getChildSpaces(parentId).map { entities ->
            entities.map { it.toModel() }
        }
    }
    
    override suspend fun getSpaceById(id: String): StorageSpace? {
        return storageSpaceDao.getSpaceById(id)?.toModel()
    }
    
    override fun getSpaceByIdFlow(id: String): Flow<StorageSpace?> {
        return storageSpaceDao.getSpaceByIdFlow(id).map { entity ->
            entity?.toModel()
        }
    }
    
    override suspend fun getSpacePath(spaceId: String): List<StorageSpace> {
        return storageSpaceDao.getSpacePath(spaceId).map { it.toModel() }
    }
    
    override fun searchSpaces(query: String): Flow<List<StorageSpace>> {
        return storageSpaceDao.searchSpaces(query).map { entities ->
            entities.map { it.toModel() }
        }
    }
    
    override suspend fun insertOrUpdateSpace(space: StorageSpace): Long {
        return storageSpaceDao.insertSpace(space.toEntity())
    }
    
    override suspend fun deleteSpace(space: StorageSpace) {
        storageSpaceDao.deleteSpace(space.toEntity())
    }
    
    override suspend fun deleteSpaceById(id: String) {
        storageSpaceDao.deleteSpaceById(id)
    }
    
    override suspend fun hasChildSpaces(spaceId: String): Boolean {
        return storageSpaceDao.hasChildSpaces(spaceId) > 0
    }
    
    override suspend fun getItemCount(spaceId: String): Int {
        return storageSpaceDao.getItemCount(spaceId)
    }
}